<template>
    <div class="contract-crm-contract-account-container">
        <el-dialog title="修改记账" v-model="isShowDialog" >
            <el-form ref="editCrmContractAccountFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col>
                        <el-form-item label="关联合同" prop="contractId">
                            <el-select v-model="formData.contractId" filterable remote 
                                @change="handContractChange"
                                @click="loadContractData()"
                                :remote-method="loadContractData" placeholder="请选择关联合同" disabled class="w100">
								<el-option 
									v-for="item in contracts"
									:key="item.id"
									:label="`（${item.contractCode}）${item.contractName}`"
									:value="item.id">
								</el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="账期序号" prop="periodNum">
                            <el-input-number v-model="formData.periodNum" :min="-9999" :max="9999" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="记账类型" prop="accountType">
                            <el-select v-model="formData.accountType" placeholder="请选择记账类型" disabled class="w100">
                                <el-option key="D" label="按天" value="D"></el-option>
                                <el-option key="W" label="按周" value="W"></el-option>
                                <el-option key="M" label="按月" value="M"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="开始时间" prop="accountStartTime">
                            <el-date-picker v-model="formData.accountStartTime" placeholder="请选择账期周期的开始时间" value-format="YYYY-MM-DD" :disabled-date="disabledPickerDate" clearable class="w100"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="结束时间" prop="accountEndTime">
                            <el-date-picker v-model="formData.accountEndTime" placeholder="请选择账期周期的结束时间" value-format="YYYY-MM-DD" :disabled-date="disabledPickerDate" clearable class="w100"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="记账主题" prop="title">
                            <el-input v-model="formData.title" placeholder="请输入记账主题" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="交付内容" prop="content">
                            <el-input type="textarea" :rows="5" v-model="formData.content" placeholder="请输入交付内容" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="开票数量" prop="invoiceNum">
                            <el-input-number v-model="formData.invoiceNum" :min="-9999" :max="9999" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="税种名称" prop="taxation">
                            <el-input v-model="formData.taxation" placeholder="请输入税种名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { editCrmContractAccount, getCrmContractAccount } from '/@/api/crm/contract/account/index';
import { getCrmContractList } from '/@/api/crm/contract/index';
export default {
    name: 'crm/contract/account/edit',
    setup(props, context) {
        const editCrmContractAccountFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            contracts: [],
            formData: {},
            formRules : {
                contractId: { required: true, message: '关联合同不能为空', trigger: 'blur' },
                periodNum: { required: true, message: '账期序号不能为空', trigger: 'blur' },
                accountType: { required: true, message: '记账类型不能为空', trigger: 'blur' },
                accountStartTime: { required: true, message: '开始时间不能为空', trigger: 'blur' },
                accountEndTime: { required: true, message: '结束时间不能为空', trigger: 'blur' },
                title: { required: true, message: '记账主题不能为空', trigger: 'blur' },
                content: { required: true, message: '交付内容不能为空', trigger: 'blur' }
            }
        });
        // 加载合同数据
        const loadContractData = (query?: string) => {
            if(query == '') return;
            getCrmContractList({contractCode: query, examineStatus: '2', contractType: '2'}).then((res) => {
                if(res.code == 0){
                    state.contracts = res.data;
                }
            });
        };
        // 选择合同
        const handContractChange = (contractId) => {
            const contract = state.contracts.find(item => item.id == contractId);
            state.formData.contractId = contract?.id;
            state.formData.contractName = contract?.contractName;
        };
        const disabledPickerDate = (time: Date) => {
            return time.getTime() > Date.now()
        }
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            if(row){
                getCrmContractAccount({id: row.id}).then((res) => {
                    if(res.code == 0){
                        state.formData = Object.assign({}, res.data);
                        state.contracts = [{id: res.data.contract?.id, contractCode: res.data.contract?.contractCode, contractName: res.data.contract?.contractName	}];
                    }
                });
            }
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            editCrmContractAccountFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            editCrmContractAccountFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        id: state.formData.id,
                        contractId: state.formData.contractId,
                        periodNum: state.formData.periodNum,
                        accountType: state.formData.accountType,
                        accountStartTime: state.formData.accountStartTime,
                        accountEndTime: state.formData.accountEndTime,
                        title: state.formData.title,
                        content: state.formData.content,
                        taxation: state.formData.taxation,
                        invoiceNum: state.formData.invoiceNum,
                        remark: state.formData.remark,
                    }
                    editCrmContractAccount(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            editCrmContractAccountFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            loadContractData,
            handContractChange,
            disabledPickerDate,
            ...toRefs(state),
        };
    },
};
</script>